import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { AuthModule } from './auth/auth.module';
import { APP_FILTER, APP_GUARD, APP_INTERCEPTOR, APP_PIPE } from '@nestjs/core';
import { AuthGuard } from './guard/auth/auth.guard';
import { HttpExceptionFilter } from './filter/http-exception/http-exception.filter';
import { ValidationPipe } from './pipe/validation/validation.pipe';
import { DemoResModule } from './demo-res/demo-res.module';
import { UsersModule } from './users/users.module';
import { TypeOrmModule } from '@nestjs/typeorm';
import { User } from './users/entities/user.entity';
import { TransformInterceptor } from './interceptor/transform/transform.interceptor';
import { ConfigModule,ConfigService } from "@nestjs/config";
import { VerifySessionModule } from './verify-session/verify-session.module';
import { DictionaryModule } from './dictionary/dictionary.module';
import { DictionaryType } from "./dictionary/entities/dictionary_type.entity";
import { DictionaryData } from "./dictionary/entities/dictionary_data.entity";
import { BannerModule } from './banner/banner.module';
import * as process from "node:process";
import * as path from "path";
import { BannerType } from "./banner/entities/banner_type.entity";
import { BannerData } from "./banner/entities/banner_data.entity";
import { UploadModule } from './upload/upload.module';
import { GoodTypeModule } from './good-type/good-type.module';
import { GoodModule } from './good/good.module';
import * as dotenv from 'dotenv';
import * as dotenvExpand from 'dotenv-expand';
import { GoodType } from './good-type/entities/good-type.entity';
import { Good } from './good/entities/good.entity';
import { SpecModule } from './spec/spec.module';
import { Spec } from './spec/entities/spec.entity';
import { SpecValue } from './spec/entities/specValue.entity';
import { SkuModule } from './sku/sku.module';
import { Sku } from './sku/entities/sku.entity';
import { FileModule } from './file/file.module';
import { OrderModule } from './order/order.module';
import { SeatModule } from './seat/seat.module';
import { Seat } from './seat/entities/seat.entity';

const env = dotenv.config();// 这会加载 .env 文件并返回一个对象
dotenvExpand.expand(env) // 这里使用 expand 方法

@Module({
  imports: [
    AuthModule,
    DemoResModule,
    UsersModule,

    ConfigModule.forRoot({
      envFilePath: path.resolve(process.cwd(),`${process.env.NODE_ENV}.env`),
      isGlobal: true, // 让配置在全局范围内可用
      expandVariables: true
    }),
    TypeOrmModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: (configService: ConfigService) => {
        return {
          type: 'mysql',
          host: configService.get<string>('HOST'),
          port: configService.get<number>('PORT'),
          username: configService.get<string>('SQL_USERNAME'),
          password: configService.get<string>('PASSWORD'),
          database: configService.get<string>('DATABASE'),
          entities: [User, DictionaryType, DictionaryData, BannerType, BannerData, GoodType, Good, Spec, SpecValue,Sku,Seat],
          synchronize: true,
        };
      }
    }),
    VerifySessionModule,
    DictionaryModule,
    BannerModule,
    UploadModule,
    GoodTypeModule,
    GoodModule,
    SpecModule,
    SkuModule,
    FileModule,
    OrderModule,
    SeatModule
  ],
  controllers: [AppController],
  providers: [
    AppService,
    {
      //全局异常过滤器
      provide: APP_FILTER,
      useClass: HttpExceptionFilter,
    },
    {
      //全局 req.user配置
      provide: APP_GUARD,
      useClass: AuthGuard,
    },
    {
      //Dto 全局管道验证
      provide: APP_PIPE,
      useClass: ValidationPipe,
    },
    {
      provide: APP_INTERCEPTOR,
      useClass: TransformInterceptor,
    },
  ]
})
export class AppModule {}
